/**
 *
 * @authors Your Name (you@example.org)
 * @date    2018-01-09 19:21:50
 * @version $Id$
 */
Ext.define('DragonApp.view.base.ChartController', {
    extend: 'Ext.app.ViewController',

    alias: 'controller.base-chart',

    requires: [
        'DragonApp.view.base.RegisterChart',
        'DragonApp.view.base.TotalChart',
        'DragonApp.view.base.DAUChart'
    ],

    /**
     * [onToggleClick description]
     * @DateTime 2018-01-09
     * @param    {[type]} button [description]
     * @param    {[type]} e [description]
     * @param    {[type]} options [description]
     * @return   {[type]}
     */
    onToggleClick: function(container, button, pressed) {

        var bar_type;

        if (pressed) {
            switch (button.text) {
                case '新增用户':
                    bar_type = 'register';
                    break;
                case '活跃用户':
                    bar_type = 'DAU';
                    break;
                case '累计用户':
                    bar_type = 'total';
                    break;
            }
        }
        this.loadStore(bar_type);
    },

    /**
     * 数据标签渲染
     * @DateTime 2018-01-10
     * @param    {int} v 数据值
     * @return   {int}   渲染
     */
    onSeriesLabelRender: function(v) {
        //return Ext.util.Format.number(v / 1000, '0,000');
        return v;
    },
    /**
     * 数据标签提示
     * @DateTime 2018-01-10
     * @param    {[type]} v [description]
     * @return   {[type]}
     */
    onSeriesTooltipRender: function(v) {
        return Ext.util.Format.number(v / 1000, '0,000');
    },

    onTabChange: function(tabPanel, newCard, oldCard, eOpts) {

        if (newCard.title === '留存数据') {

            var _form = Ext.ComponentQuery.query('basequery')[0];

            var form = _form.getForm();

            if (!form.isValid()) return;

            var formValue = form.getValues();

            var grid = Ext.ComponentQuery.query('basegrid')[0];
            var store = grid.getStore();


            //根据查询附加参数加载数据仓库

            store.load({
                params: formValue,
                scope: this,
                callback: function(records, operation, success) {

                    if (success == false) {
                        store.removeAll();
                    }
                }
            });
        } else if (newCard.title === '用户数据') {
            this.loadStore();
        }
    },

    loadStore: function(data_field) {

        //默认参数
        //
        var _data_field = arguments[0] ? arguments[0] : null;

        var _form = Ext.ComponentQuery.query('basequery')[0];

        var form = _form.getForm();

        if (!form.isValid()) return;

        var formValue = form.getValues();

        var panel = Ext.ComponentQuery.query('basepanel')[0];

        var bar_chart;

        var registerChart = Ext.create('DragonApp.view.base.RegisterChart', {
            title: '新增用户'
        });
        var totalChart = Ext.create('DragonApp.view.base.TotalChart', {
            title: '累计用户'
        });
        var DAUChart = Ext.create('DragonApp.view.base.DAUChart', {
            title: '活跃用户'
        });
        var store;

        switch (_data_field) {
            case null:
            case 'register':
                store = registerChart.getStore();
                bar_chart = registerChart;
                break;
            case 'total':
                store = totalChart.getStore();
                bar_chart = totalChart;
                break;
            case 'DAU':
                store = DAUChart.getStore();
                bar_chart = DAUChart;
                break;
        }

        store.load({
            params: formValue,
            scope: this,
            callback: function(records, operation, success) {

                if (success == false) {
                    store.removeAll();
                } else if (success == true) {
                    panel.removeAll();
                    panel.add(bar_chart);
                }

            }
        });
    }
});